//
//  EScrollerView.m
//  icoiniPad
//
//  Created by Ethan on 12-11-24.
//
//

#import "EScrollerViewEx.h"
@implementation EScrollerViewEx
@synthesize delegate;

-(id)initWithFrameRect:(CGRect)rect subViewsArray:(NSArray *)viewArr
{
    
	if ((self=[super initWithFrame:rect])) {
        self.userInteractionEnabled=YES;

		contentArray=[NSArray arrayWithArray:viewArr];
		viewSize=rect;
        NSUInteger pageCount=[viewArr count];
        scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewSize.size.width, viewSize.size.height)];
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(viewSize.size.width * pageCount, viewSize.size.height);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.scrollsToTop = NO;
        scrollView.delegate = self;
        scrollView.bounces = NO;
      
        for (int i=0; i<pageCount; i++) {
            
            UIView* sub = [viewArr objectAtIndex:i];
            
            [sub setFrame:CGRectMake(viewSize.size.width*i, 0,viewSize.size.width, viewSize.size.height)];
            sub.tag=i;
            UITapGestureRecognizer *Tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressed:)];
            [Tap setNumberOfTapsRequired:1];
            [Tap setNumberOfTouchesRequired:1];
            sub.userInteractionEnabled=YES;
            [sub addGestureRecognizer:Tap];
            [scrollView addSubview:sub];
        }
        
        [self addSubview:scrollView];
        setPageIndex = 0;
        
        
        //说明文字层
        UIView *noteView=[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-33,self.bounds.size.width,33)];
        [noteView setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.0]];
        
        float pageControlWidth= pageCount*10.0f+40.f;
        float pagecontrolHeight=20.0f;
        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.frame.size.width - pageControlWidth) / 2,6, pageControlWidth, pagecontrolHeight)];
        pageControl.currentPage=0;
        pageControl.numberOfPages=(pageCount);
        [noteView addSubview:pageControl];
        
        noteTitle=[[UILabel alloc] initWithFrame:CGRectMake(5, 6, self.frame.size.width-pageControlWidth-15, 20)];
        [noteTitle setText:[titleArray objectAtIndex:0]];
        [noteTitle setBackgroundColor:[UIColor clearColor]];
        [noteTitle setFont:[UIFont systemFontOfSize:13]];
        [noteView addSubview:noteTitle];
        
        [self addSubview:noteView];
        
    }
	return self;
}


- (void)viewDidLoad
{
    
    NSLog(@"view did load");
    
    // Do any additional setup after loading the view.
}


- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    currentPageIndex=page;
    
    pageControl.currentPage= page;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
//    if (currentPageIndex==0) {
//        if (setPageIndex != currentPageIndex) {
//            
//            [_scrollView setContentOffset:CGPointMake(0, 0)];
//        }else{
//        
//            [_scrollView setContentOffset:CGPointMake(([contentArray count]-1) * viewSize.size.width, 0)];
//            
//            pageControl.currentPage= [contentArray count]-1;
//            currentPageIndex = [contentArray count]-1;
//        }
//        
//    }else
//    if (currentPageIndex <= ([contentArray count]-1)) {
//       
//        [_scrollView setContentOffset:CGPointMake(viewSize.size.width * currentPageIndex, 0)];
//        
//    }
//    
//    setPageIndex = currentPageIndex;
    
    

}
- (void)imagePressed:(UITapGestureRecognizer *)sender
{

    if ([delegate respondsToSelector:@selector(EScrollerViewDidClicked:)]) {
        [delegate EScrollerViewDidClicked:sender.view.tag];
    }
}

@end
